from .zjMysql import zjMySQL
class ORM():
    def save(self):
                #insert into students(name,age) values ("小李"，22)
        tableName = (self.__class__.__name__).lower()  #表名
                #self.__class__获取当前的类；__name__获取当前调用的对象的类名；lower()转小写
        fieldsStr =valuesStr="("     #fieldsStr代表(name,age)；valuesStr代表("小李"，22)
        for field in self.__dict__:   #self.__dict__得到字典的键值对{"小李"：22，"小白"：23}中的属性名
            fieldsStr += (field + ",")      #循环1(name, ;循环2 (name,age，
            if isinstance(self.__dict__[field],str):    #判断key-valuede中value值是否是字符串
                valuesStr += ("'"+self.__dict__[field]+"',")
            else:                               #不是则为数字
                valuesStr +=(str(self.__dict__[field])+",") #转为字符串

        fieldsStr = fieldsStr[:len(fieldsStr)-1]+")"        #截取name (name,age,之前，就把逗号去掉，拼接“）”变成(name,age）
        valuesStr = valuesStr[:len(valuesStr)-1]+")"        #一样的截取掉后面的逗号，("小李"，22)
        sql = "insert into " +tableName +" "+ fieldsStr+"values "+valuesStr
        db = zjMySQL()
        db.insert(sql)

    def delete(self):
        pass

    def update(self):
        pass

    @classmethod    #装饰为类方法，类名来调用
    def all(cls):
        #select * from students
        tableName = (cls.__name__).lower()
        sql = "select * from " +tableName
        db = zjMySQL()
        return  db.get_all_obj(sql,tableName)

    @classmethod
    def filter(cls):
        pass